<?php
include('functions.inc.php');
$link = db_connect();

header("Content-type: text/html; charset=UTF-8");

$article_query = sprintf("SELECT a.*, s.title AS subTitle, a.subId AS subId FROM articles a LEFT JOIN subscriptions s ON (a.subId = s.id) WHERE a.id = %s",
	mysql_real_escape_string($_GET['id'])
);

$result = mysql_query($article_query, $link)
	or die("Can't load article: " . mysql_error() . "\n");

while ($article = mysql_fetch_array($result)) {
	$id = $article['id'];
	$title = $article['title'];
	$subTitle = $article['subTitle'];
	$subId = $article['subId'];
	$description = $article['description'];
	$alink = $article['link'];
	$date = date("Y-m-d H:i",strtotime($article['pubDate'])+7*60*60);
	$state = $article['state'];

	// Post-processing
	$post_processing_query = sprintf("SELECT from_re, to_re FROM subs_post_processing WHERE subId = %s ORDER BY rank",
		$article['subId']
	);
	$result2 = mysql_query($post_processing_query, $link)
		or die("Can't load post-processing: " . mysql_error() . "\n");
	while ($post_processing_re = mysql_fetch_array($result2)) {
		$description = ereg_replace($post_processing_re['from_re'], $post_processing_re['to_re'], $description);
	}

	echo "$id;";
	?>
<div id="articleBlock"><a id="currentLink" href="<?php echo $alink ?>" onclick="openCurentLink()"><span class="articleTitle"><?php echo $title ?></span></a></div>
<div class="channel" style="cursor: default">
	<?php echo $date ?>
	<?php echo (!empty($subTitle) ? ' - ' . $subTitle : '') ?>
	[<a id="feed_actions_link" href="show_feed_actions" onclick="show_feed_actions('<?php echo str_replace("'","\'", $subTitle) ?>', <?php echo $subId ?>);return false">a</a><a id="feed_actions_hide_link" style="display: none" href="hide_feed_actions" onclick="hide_feed_actions();return false">a</a>]
	<?php
	if (strstr($state,'starred') !== false) {
		echo ' - <span id="starAction" class="actionDone" onclick="starCurrent()">starred</span>';
	} else {
		echo ' - <span id="starAction" class="action" onclick="starCurrent()">star this!</span>';
	}
	if (strstr($state,'unread') !== false) {
		echo ' - <span id="unreadAction" class="actionDone" onclick="keepUnreadCurrent()">unread</span>';
	} else {
		echo ' - <span id="unreadAction" class="action" onclick="keepUnreadCurrent()">keep this unread</span>';
	}
	?>
</div>
<div class="article" id="article_body"><?php echo $description ?></div>
	<?php
}

mysql_close($link);
?>